//
//  NewsViewController.m
//  CarHome
//
//  Created by apple on 15/3/21.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "NewsViewController.h"
#import "UIButton+InitButton.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "NewsPaperViewController.h"

@interface NewsViewController ()<UIScrollViewDelegate>
@property (nonatomic, retain)UIScrollView *scroll;
@property (nonatomic, retain)UIPageControl *pageControl;
@property (nonatomic, retain)UIView *myView;
@property (nonatomic, assign)BOOL flag;
@property (nonatomic, retain)id result;
@end

@implementation NewsViewController
- (void)dealloc
{
    [_scroll release];
    [_pageControl release];
    [_myView release];
    [_result release];
    [super dealloc];
}
- (void)loadData
{
    NSString *str = @"http://app.api.autohome.com.cn/autov4.6/news/newslist-a2-pm2-v4.6.1-c0-nt0-p1-s20-l0.html";
    NSString *url = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *str = [NSString stringWithString:operation.responseString];
        NSData *resData = [[NSData alloc] initWithData:[str dataUsingEncoding:NSUTF8StringEncoding]];
        self.result = [NSJSONSerialization JSONObjectWithData:resData options:NSJSONReadingMutableContainers error:nil];
//        NSLog(@"%@", self.result);
        
        NSLog(@"%ld", [[[self.result objectForKey:@"result"]objectForKey:@"focusimg"] count]);
        
        
        for (NSInteger i = 0; i < [[[self.result objectForKey:@"result"]objectForKey:@"focusimg"] count]; i++) {
            UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(i * self.scroll.frame.size.width - 1, 0, self.scroll.frame.size.width, self.scroll.frame.size.height)];
            [self.scroll addSubview:imageview];
            self.scroll.delegate = self;
            
            NSString *urlStr = [[[[self.result objectForKey:@"result"]objectForKey:@"focusimg"]objectAtIndex:i]objectForKey:@"imgurl"];
           
            NSURL *url = [NSURL URLWithString:urlStr];
            [imageview setImageWithURL:url];
            
            imageview.backgroundColor = [UIColor cyanColor];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"数据加载失败");
    }];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self loadData];
    
    _timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
    [[NSRunLoop  currentRunLoop] addTimer:_timer forMode:NSDefaultRunLoopMode];
//    [_timer setFireDate:[NSDate distantFuture]];//关闭定时器
    [_timer fire];
    
    
    self.flag = YES;
    self.scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
    [self.view addSubview:self.scroll];
    self.scroll.contentSize = CGSizeMake(self.scroll.frame.size.width * 6, self.scroll.bounds.size.height - 200);
    self.scroll.showsHorizontalScrollIndicator = NO;
    self.scroll.showsVerticalScrollIndicator = NO;
    self.scroll.contentOffset = CGPointMake(0, 64);
    self.scroll.pagingEnabled = YES;
    
//    self.scroll.showsHorizontalScrollIndicator = NO;
//    self.scroll.showsVerticalScrollIndicator = NO;
//    self.scroll.backgroundColor = [UIColor greenColor];
    [_scroll release];
   
    
    
    
    
    self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, self.scroll.frame.size.height + self.scroll.frame.origin.y, self.view.frame.size.width, 20)];
    [self.pageControl addTarget:self action:@selector(pageControlAction:) forControlEvents:UIControlEventValueChanged];
    self.pageControl.backgroundColor = [UIColor blackColor];
    self.pageControl.numberOfPages = 6;
    [self.view addSubview:self.pageControl];
    [_pageControl release];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, self.pageControl.frame.size.height + self.pageControl.frame.origin.y, self.view.frame.size.width, 20)];
    [self.view addSubview:label];
    label.text = @"新车展示";
    label.textAlignment = NSTextAlignmentCenter;
    [label release];
    UIImageView *imageViewLogo = [[UIImageView alloc]initWithFrame:CGRectMake(0, label.frame.size.height + label.frame.origin.y, self.view.frame.size.width, 300)];
    NSString *imageStr = @"http://img.365ta.com/forum/201104/12/004455z9yqfw7qsqsaqfzl.jpg";
    
    NSURL *url = [NSURL URLWithString:imageStr];
    NSLog(@"%@", imageStr);
    [imageViewLogo setImageWithURL:url];
    
    [self.view addSubview:imageViewLogo];
    [imageViewLogo release];
    
    
    self.myView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, self.pageControl.frame.size.height + self.pageControl.frame.origin.y + 20, self.view.frame.size.width, self.view.frame.size.height - 64 - 49 - self.scroll.frame.size.height - self.pageControl.frame.size.height)];
//    self.myView.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:self.myView];
    [_myView release];
    
    
    UIButton *NewsButton = [UIButton buttonWithTitle:nil frame:CGRectMake(0, 0, self.myView.frame.size.width / 2 , self.myView.frame.size.height / 2 ) target:self action:@selector(newsButtonAction:)];
    UIButton *NewsButtonIn = [UIButton buttonWithTitle:nil frame:CGRectMake(40, 40, NewsButton.frame.size.width - 80, NewsButton.frame.size.height - 80) target:self action:@selector(newsButtonAction:)];
    [NewsButton addSubview:NewsButtonIn];
    [NewsButtonIn setBackgroundImage:[UIImage imageNamed:@"news.png"] forState:UIControlStateNormal];
    [self.myView addSubview:NewsButton];
    
    
    
    UIButton *UseCarButton = [UIButton buttonWithTitle:nil frame:CGRectMake(self.view.frame.size.width / 2, 0, self.myView.frame.size.width / 2, self.myView.frame.size.height / 2 ) target:self action:@selector(UseCarButtonAction:)];
    UIButton *UseCarButtonIn = [UIButton buttonWithTitle:nil frame:CGRectMake(40, 40, NewsButton.frame.size.width - 80, NewsButton.frame.size.height - 80) target:self action:@selector(UseCarButtonAction:)];
    [UseCarButton addSubview:UseCarButtonIn];
    [UseCarButton setBackgroundImage:[UIImage imageNamed:@"car.png"] forState:UIControlStateNormal];
    [self.myView addSubview:UseCarButton];
    
    
    UIButton *recommendButton = [UIButton buttonWithTitle:nil frame:CGRectMake(0, self.myView.frame.size.height / 2, self.myView.frame.size.width / 2, self.myView.frame.size.height / 2) target:self action:@selector(recommendButtonAction:)];
    UIButton *recommendButtonIn = [UIButton buttonWithTitle:nil frame:CGRectMake(40, 40, NewsButton.frame.size.width - 80, NewsButton.frame.size.height - 80) target:self action:@selector(recommendButtonAction:)];
    [recommendButton addSubview:recommendButtonIn];
    [recommendButtonIn setBackgroundImage:[UIImage imageNamed:@"recommend.png"] forState:UIControlStateNormal];
    [self.myView addSubview:recommendButton];
    
    UIButton *infoButton = [UIButton buttonWithTitle:nil frame:CGRectMake(self.myView.frame.size.width / 2, self.myView.frame.size.height / 2, self.myView.frame.size.width / 2, self.myView.frame.size.height / 2) target:self action:@selector(infoButtonAction:)];
    UIButton *infoButtonIn = [UIButton buttonWithTitle:nil frame:CGRectMake(40, 40, NewsButton.frame.size.width - 80, NewsButton.frame.size.height - 80) target:self action:@selector(infoButtonAction:)];
    [infoButton addSubview:infoButtonIn];
    infoButtonIn.layer.cornerRadius = 20;
    infoButtonIn.layer.borderWidth = 2;
    
    
    [infoButtonIn setBackgroundImage:[UIImage imageNamed:@"info.png"] forState:UIControlStateNormal];
    [self.myView addSubview:infoButton];
    
    UIButton *deleteButton = [UIButton buttonWithTitle:nil frame:CGRectMake(self.myView.frame.size.height / 2 - 20, 0, 40, 40) target:self action:@selector(deleteButtonAction:)];
    
    [deleteButton setBackgroundImage:[UIImage imageNamed:@"delete.png"] forState:UIControlStateNormal];
    [UseCarButton addSubview:deleteButton];
   
    
    //这个是toolbar上面的按钮
    UIBarButtonItem *tabBarButton = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"up.png"] style:UIBarButtonItemStylePlain target:self action:@selector(tabBarButtonAction:)];
    NSArray *array = @[tabBarButton];
    [tabBarButton release];
    self.toolbarItems = array;
    
    
//   deleteButton.backgroundColor = [UIColor greenColor];
//    NewsButton.backgroundColor = [UIColor yellowColor];
//    UseCarButton.backgroundColor = [UIColor redColor];
//    infoButton.backgroundColor = [UIColor blueColor];
//    recommendButton.backgroundColor = [UIColor orangeColor];
//    deleteButton.backgroundColor = [UIColor greenColor];
   
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setToolbarHidden:YES animated:YES];
    self.navigationController.navigationBar.translucent = NO;
    
}
- (void)viewWillDisappear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:NO];
}
#pragma mark button的点击事件
- (void)newsButtonAction:(UIBarButtonItem *)button
{
    NSLog(@"newsButtonAction");
    NewsPaperViewController *newsPage = [[NewsPaperViewController alloc]init];
    [self.navigationController pushViewController:newsPage animated:YES];
    
    newsPage.result = self.result;
    [newsPage release];

}
- (void)UseCarButtonAction:(UIBarButtonItem *)button
{
    NSLog(@"UseCarButtonAction");
}
- (void)recommendButtonAction:(UIBarButtonItem *)button
{
    NSLog(@"recommendButtonAction");
}
- (void)infoButtonAction:(UIBarButtonItem *)button
{
    NSLog(@"infoButtonAction");
}
- (void)deleteButtonAction:(UIBarButtonItem *)button
{
    NSLog(@"deleteButtonAction");
        [UIView animateWithDuration:1.0 animations:^{
            self.myView.frame = CGRectMake(0, 667, self.view.frame.size.width, 0);
            [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
            [self.navigationController setToolbarHidden:NO animated:YES];
        }];
}
- (void)tabBarButtonAction:(UIBarButtonItem *)button
{
    
    [UIView animateWithDuration:1.0 animations:^{
        self.myView.frame = CGRectMake(0, self.pageControl.frame.size.height + self.pageControl.frame.origin.y + 20, self.view.frame.size.width, self.view.frame.size.height - 64 - self.scroll.frame.size.height - self.pageControl.frame.size.height);
        [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [self.navigationController setToolbarHidden:YES animated:YES];
    }];
    
}
#pragma mark uiscrollView协议方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.pageControl.currentPage = self.scroll.contentOffset.x / self.scroll.frame.size.width;
    
    
}
#pragma mark pageControl 点击方法
- (void)pageControlAction:(UIPageControl *)pageControl
{
  
    [self.scroll setContentOffset:CGPointMake(self.pageControl.currentPage * self.scroll.frame.size.width, 0) animated:YES];
}
#pragma mark timerAction:
- (void)timerAction:(NSTimer *)timer
{
    [self.scroll setContentOffset:CGPointMake(self.scroll.contentOffset.x + self.scroll.frame.size.width, self.scroll.frame.origin.y) animated:YES];
    if (self.scroll.contentOffset.x > 375 * 4) {
        
        [self.scroll setContentOffset:CGPointMake(0, 0) animated:YES];
        self.pageControl.currentPage = 0;
        return;
        
    }
     self.pageControl.currentPage = self.scroll.contentOffset.x / self.scroll.frame.size.width + 1;
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
